﻿using System.Collections.Generic;
using System;

namespace HREngine.Bots
{
    public partial class Behavior咆哮德 : Behavior
    {
        public override string BehaviorName() { return "咆哮德"; }
        PenalityManager penman = PenalityManager.Instance;
        // 核心，场面值
        public override float getPlayfieldValue(Playfield p)
        {
            float retval = 0;
            retval += getGeneralVal(p);
            retval += p.owncarddraw * 3;
            // 危险血线
            int hpboarder = 10;
            // 抢脸血线
            int aggroboarder = 15;
            retval += getHpValue(p, hpboarder, aggroboarder);
            // 出牌序列数量
            int count = p.playactions.Count;
            int ownActCount = 0;
            // 排序问题！！！！
            for (int i = 0; i < count; i++)
            {
                Action a = p.playactions[i];
                ownActCount++;
                switch (a.actionType)
                {
                    // 英雄攻击
                    case actionEnum.attackWithHero:
                        continue;
                    case actionEnum.useHeroPower:
                    case actionEnum.playcard:
                        break;
                    default:
                        continue;
                }
                switch (a.card.card.chnName)
                {
                    // 排序优先
                    case "自然之怒":
                        retval -= i * 2;
                        break;
                    case "碧蓝幼龙":
                    case "紫罗兰教师":
                        retval -= i * 2;
                        break;
                }
            }
            retval += getSecretPenality(p); // 奥秘的影响
            p.value = retval;
            return retval;
        }


        // 敌方随从价值 主要等于 
        public override int getEnemyMinionValue(Minion m, Playfield p)
        {
            int retval = 5;
            // 太大不管了
            retval += m.Hp * 2 > 10 ? 10 : m.Hp * 2;
            if (!m.frozen && !(m.cantAttack && m.name != CardDB.cardName.argentwatchman))
            {
                retval += m.Angr * 2;
                if (m.windfury) retval += m.Angr * 2;
                if (m.Angr > 4) retval += 20;
                if (m.Angr > 7) retval += 20;
            }
            if (m.silenced) return retval;

            if (!m.handcard.card.isSpecialMinion)
            {
                if (m.Angr == 0) retval -= 7;
                else if (m.Angr <= 2 && m.Hp <= 2 && !m.divineshild) retval -= 5;
            }
            
            if (m.taunt) retval += 1;
            if (m.divineshild) retval += m.Angr;
            if (m.divineshild && m.taunt) retval += 5;
            if (m.stealth) retval += 1;

            if (m.poisonous)
            {
                retval += 4;
                if (p.ownMinions.Count < p.enemyMinions.Count) retval += 10;
            }
            if (m.lifesteal) retval += m.Angr;

            if (m.handcard.card.targetPriority >= 1 && !m.silenced)
            {
                retval += m.handcard.card.targetPriority;
            }
            if (m.name == CardDB.cardName.nerubianegg && m.Angr <= 3 && !m.taunt) retval = 0;
            retval += m.synergy;
            switch (m.handcard.card.chnName){
                // 解不掉游戏结束
                case "加基森拍卖师":
                case "任务达人":
                case "苔原犀牛":
                case "飞刀杂耍者":
                case "迦顿男爵":
                case "法力之潮图腾":
                case "巫师学徒":
                case "火舌图腾":
                    retval += 100;
                    break;
                // 解不掉大劣势
                case "大法师安东尼达斯":
                    retval += 30;
                    break;
                // 我劝你最好解了
                case "苦痛侍僧":
                case "北郡牧师":
                case "狂野炎术士":
                case "奥金尼灵魂祭司":
                case "暴风城勇士":
                case "法力浮龙":
                case "雷欧克":
                case "森林狼":
                case "饥饿的秃鹫":
                case "恐狼前锋":
                case "食腐土狼":
                case "先知维纶":
                case "炎魔之王拉格纳罗斯":
                case "铸甲师":
                    retval += 20;
                    break;
                // 带点异能随手解一下吧
                case "末日预言者":
                case "年轻的女祭司":
                    retval += 3;
                    break;
                // 大哥，求别解
                case "希尔瓦娜斯":
                    retval -= 10;
                    break;                
            }
            return retval;
        }

        public override int getMyMinionValue(Minion m, Playfield p)
        {
            int retval = 2;
            retval += m.Hp * 2;
            if (!m.cantAttack || !m.Ready || !m.frozen)
            {
                retval += m.Angr;
            }
            else
            {
                retval += m.Angr / 4;
            }
            // 鱼人种族价值
            //if (m.handcard.card.race == 14)
            //{
            //    retval += m.Hp + m.Angr + 2;
            //}
            // 风怒价值
            if ((!m.playedThisTurn || m.rush == 1 || m.charge == 1) && m.windfury) retval += m.Angr;
            // 圣盾价值
            if (m.divineshild ) retval += m.Angr / 2 + 1;
            // 潜行价值
            if (m.stealth) retval += m.Angr / 3 + 1;
            // 吸血
            if (m.lifesteal) retval += m.Angr / 3 + 1;
            // 圣盾嘲讽
            if (m.divineshild && m.taunt) retval += 4;
            // retval += m.synergy;
            // retval += getSpeicalCardVal(m.handcard.card);
            switch (m.handcard.card.chnName)
            {
                // 选择加血
                case "利爪德鲁伊":
                    if (m.charge > 0) retval -= 10;
                    else if (m.taunt) retval += 5;
                    break;
            }
            return retval;
        }

        public override int getSirFinleyPriority(List<Handmanager.Handcard> discoverCards)
        {
            
            return -1; //comment out or remove this to set manual priority
            int sirFinleyChoice = -1;
            int tmp = int.MinValue;
            for (int i = 0; i < discoverCards.Count; i++)
            {
                CardDB.cardName name = discoverCards[i].card.name;
                if (SirFinleyPriorityList.ContainsKey(name) && SirFinleyPriorityList[name] > tmp)
                {
                    tmp = SirFinleyPriorityList[name];
                    sirFinleyChoice = i;
                }
            }
            return sirFinleyChoice;
        }
        public override int getSirFinleyPriority(CardDB.Card card)
        {
            return SirFinleyPriorityList[card.name];
        }

        private Dictionary<CardDB.cardName, int> SirFinleyPriorityList = new Dictionary<CardDB.cardName, int>
        {
            //{HeroPowerName, Priority}, where 0-9 = manual priority
            { CardDB.cardName.lesserheal, 0 }, 
            { CardDB.cardName.shapeshift, 6 },
            { CardDB.cardName.fireblast, 7 },
            { CardDB.cardName.totemiccall, 1 },
            { CardDB.cardName.lifetap, 9 },
            { CardDB.cardName.daggermastery, 5 },
            { CardDB.cardName.reinforce, 4 },
            { CardDB.cardName.armorup, 2 },
            { CardDB.cardName.steadyshot, 8 }
        };

        public override int getComboPenality(CardDB.Card card, Minion target, Playfield p)
        {
            // 无法选中
            if (target != null && target.untouchable)
            {
                return 100000;
            }
            // 初始惩罚值
            int pen = 0;
            pen += penman.getSilencePenality(card.name, target, p);
            switch (card.chnName)
            {
                case "横扫":
                    if(p.enemyMinions.Count < 2)
                    {
                        pen += 10;
                    }
                    break;
                case "野性成长":
                    if (p.ownMaxMana <= 3) pen -= 20;
                    else if (p.ownMaxMana == 9) pen = 300;
                    break;
                case "森金持盾卫士":
                case "冰风雪人":
                    if (p.ownMaxMana < 4)
                        pen -= 20;
                    break;
                case "紫罗兰教师":
                    if(p.enemyMinions.Count == 0)
                        pen -= 11;
                    break;
                case "野性之力":
                    if (p.ownMaxMana < 3 || p.ownMinions.Count < 3)
                        pen += 4;
                    break;
                // 丛林守护者
                case "月火术":
                    if(p.enemyMinions.Count == 0 || (target != null && target.Hp > 2 ) )
                        pen += 20;
                    break;
                case "禁魔":
                    if (target != null)
                        pen -= 3 * ((target.Hp - target.handcard.card.Health > 0 ? target.Hp - target.handcard.card.Health : 0)
                            + (target.Angr - target.handcard.card.Attack > 0 ? target.Angr - target.handcard.card.Attack : 0));
                    break;
                case "幸运币":
                    pen += 5;
                    break;
                case "激活":
                    pen += 12;
                    if(p.ownMaxMana > 5)
                    {
                        pen -= 12;
                    }
                    if (p.mana > 8) pen += 1000;
                    break;
                case "自然之力":
                    foreach(Handmanager.Handcard hc in p.owncards)
                    {
                        if(hc.card.type == CardDB.cardtype.MOB)
                        {
                            pen += 20;
                        }
                        if (hc.card.chnName == "野蛮咆哮" && p.mana < 9)
                        {
                            pen += 40;
                        }
                    }
                    break;
                case "摄取养分":
                    foreach (Handmanager.Handcard hc in p.owncards)
                    {
                        if (hc.card.type == CardDB.cardtype.MOB && hc.manacost <= 5)
                        {
                            pen += 20;
                        }
                    }
                    break;
                case "野蛮咆哮":
                    pen += 40;
                    foreach(Minion m in p.ownMinions)
                    {
                        if (m.Ready) pen -= 9;
                    }
                    foreach (Handmanager.Handcard hc in p.owncards)
                    {
                        if (hc.card.chnName == "自然之力" )
                        {
                            pen += 10;
                            break;
                        }
                    }
                    break;
                case "王牌猎人":
                    if(target == null || target.Angr < 7)
                    {
                        pen += 10;
                    }else
                    {
                        pen -= 20;
                    }
                    break;
                case "战利品贮藏者":
                case "麦田傀儡":
                    pen -= 5;
                    break;
                case "阿古斯防御者":
                    if( p.ownMinions.Count > 1) pen -= 5;
                    if (p.ownMinions.Count > 2) pen -= 2;
                    break;
                case "叫嚣的中士":
                    if (target == null || !target.Ready) pen += 5;
                    break;
                case "黑铁矮人":
                    if (target == null || !target.Ready) pen += 3;
                    break;
                case "黑骑士":
                    if (target != null) pen -= 20;
                    break;
                case "希尔瓦娜斯·风行者":
                    if (p.enemyMinions.Count > 3) pen -= 20;
                    break;
                case "凯恩·血蹄":
                    pen -= 10;
                    break;
                case "古老的秘密":
                    if(p.ownHero.Hp <= 8 && target != null && target.isHero)
                    {
                        pen -= 10;
                    }
                    break;
                //-----------------------------英雄技能-----------------------------------
                case "变形":
                    pen -= 1;
                    if(p.ownMaxMana < 3)
                    foreach(Minion m in p.enemyMinions)
                    {
                        if (m.untouchable || m.stealth) continue;
                        if(m.Hp == 1 && m.Angr < 5)
                        {
                            pen = -12;
                        }
                        if (m.taunt && m.Hp != 1)
                        {
                            pen = -1;
                            break;
                        }
                    }
                    break;
            }
            return pen;
        }

        public override int getHpValue(Playfield p, int hpboarder, int aggroboarder)
        {
            int retval = 0;
            // 血线安全
            if (p.ownHero.Hp + p.ownHero.armor > hpboarder)
            {
                retval += (5 + p.ownHero.Hp + p.ownHero.armor - hpboarder) / 2;
            }
            // 快死了
            else
            {
                if (p.nextTurnWin()) retval -= (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor);
                else retval -= 5 * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor) * (hpboarder + 1 - p.ownHero.Hp - p.ownHero.armor);
            }
            // 对手血线安全
            if (p.enemyHero.Hp + p.enemyHero.armor > aggroboarder)
            {
                retval += 3 - p.enemyHero.Hp - p.enemyHero.armor + aggroboarder;
            }
            // 开始打脸
            else if (p.enemyHero.Hp + p.enemyHero.armor <= aggroboarder && p.enemyHero.Hp + p.enemyHero.armor > aggroboarder / 3)
            {
                retval += 3 * (aggroboarder + 1 - p.enemyHero.Hp - p.enemyHero.armor);
            }
            else
            {
                retval += 4 * (aggroboarder + 1 - p.enemyHero.Hp - p.enemyHero.armor);
            }
            return retval;
        }
    }
}